package com.boot.app.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.system.domain.SystemParam;
import com.boot.app.system.service.ISystemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/param")
public class SystemParamController {

    private final ISystemParamService service;

    @Autowired
    public SystemParamController(ISystemParamService service){
        this.service = service;
    }

    @GetMapping("{id}")
    public ApiResult<SystemParam> getParam(@PathVariable("id") long id){
        SystemParam systemParam = service.getById(id);
        return ApiResult.success(systemParam);
    }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<SystemParam>> getParamPage(@PathVariable("page")int page, @PathVariable("size")int size, SystemParam systemParam){
        LambdaQueryWrapper<SystemParam> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(systemParam.getCode())) queryWrapper.like(SystemParam::getCode, systemParam.getCode());
        if(StrUtil.isNotBlank(systemParam.getName())) queryWrapper.like(SystemParam::getName, systemParam.getName());
        Page<SystemParam> paramPage = service.page(new Page<>(page,size),queryWrapper);
        return ApiResult.success(paramPage);
    }

    @PostMapping()
    public ApiResult<?> postOrPutParam(@RequestBody SystemParam systemParam){
        service.saveOrUpdate(systemParam);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteParam(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
